// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Current Tunnel Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 42 : TYPE_CURRENTTUNNEL


// Static Values
public value CurrentTunnel_value33 = 0;

// Tables

event ObjectMain
	temp6 = 0
	temp7 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckLower(object[currentPlayer].xpos, object.value0)
		temp0 = checkResult
		CheckGreater(object[currentPlayer].xpos, object.value1)
		temp0 |= checkResult
		CheckLower(object[currentPlayer].ypos, object.value2)
		temp0 |= checkResult
		CheckGreater(object[currentPlayer].ypos, object.value3)
		temp0 |= checkResult
		if temp0 == 0
			temp7 = 1
			if CurrentTunnel_value33 == 0
				temp6 = 1
				CheckEqual(object[currentPlayer].state, PlayerObject_Hurt)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Knockback)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Death)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Drown)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Clinging)
				temp0 |= checkResult
				if temp0 == 0
					object[currentPlayer].state = PlayerObject_Clinging
					object[currentPlayer].animation = ANI_FANROTATE
					object[currentPlayer].tileCollisions = 1
				end if
				if object[currentPlayer].animation == ANI_FANROTATE
					if object[currentPlayer].xvel == 0
						if object.propertyValue == 0
							if object[currentPlayer].ypos < object.value7
								object.value7 += 0x80000
								if object.value7 > object.value3
									object.value7 = object[currentPlayer].ypos
									object.value7 -= 0x40000
								end if
								object[currentPlayer].ypos += 0x40000
							else
								object.value7 -= 0x80000
								if object.value7 < object.value2
									object.value7 = object[currentPlayer].ypos
									object.value7 += 0x40000									
								end if
								object[currentPlayer].ypos -= 0x40000
							end if
						end if
					else
						object.value7 = object.value3
						object.value7 -= object.value2
						object.value7 >>= 1
						object.value7 += object.value2	
					end if
				end if
			else
				CurrentTunnel_value33--
				if object[currentPlayer].state == PlayerObject_Clinging
					object[currentPlayer].state = PlayerObject_HandleAir
					object[currentPlayer].xvel = 0x40000
					object[currentPlayer].speed = 0x40000
				end if
			end if
		else
			if object[currentPlayer].state == PlayerObject_Clinging
				object[currentPlayer].state = PlayerObject_HandleAir
				object[currentPlayer].xvel = 0x40000
				object[currentPlayer].speed = 0x40000
			end if
		end if
	next
	object.priority = temp6
	if object.propertyValue == 0
		object[+1].priority = temp6
	else
		object[-1].priority = temp6
	end if
	if temp7 == 0
		object.value4 = 0
	end if
	if object.value4 != 0
		object.value4--
		if temp7 == 1
			if object.value4 <= 0
				if object.propertyValue == 0
					temp0 = object[+1].value4
				else
					temp0 = object[-1].value4
				end if
				if temp0 == 0
					StopSfx(SfxName[Waterfall])
					PlaySfx(SfxName[Waterfall Loop], 0)
					object.value4 = 31
				end if
			end if
		end if
	else
		if temp7 == 1
			if object.propertyValue == 0
				temp0 = object[+1].value4
			else
				temp0 = object[-1].value4
			end if
			if temp0 == 0
				PlaySfx(SfxName[Waterfall], 0)
				object.value4 = 31
			end if
		end if
	end if
end event

event ObjectDraw
	if object[0].type == TypeName[Debug Mode]
		DrawSpriteXY(0, object.value0, object.value2)
		DrawSpriteXY(0, object.value0, object.value3)
		DrawSpriteXY(1, object.value1, object.value2)
		DrawSpriteXY(1, object.value1, object.value3)
	end if
end event

event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 84, 173) //0 A
	SpriteFrame(-8, -8, 16, 16, 101, 173) //0 A
	CurrentTunnel_value33 = 0
	foreach (TypeName[Current Tunnel], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 6
		if object[arrayPos0].propertyValue == 0
			arrayPos1 = arrayPos0
			arrayPos1++
			object[arrayPos0].value0 = object[arrayPos0].xpos
			object[arrayPos0].value1 = object[arrayPos1].xpos
			temp1 = object[arrayPos0].ypos
			temp2 = object[arrayPos0].ypos
		else
			arrayPos1 = arrayPos0
			arrayPos1--
			object[arrayPos0].value0 = object[arrayPos1].xpos
			object[arrayPos0].value1 = object[arrayPos0].xpos
			temp1 = object[arrayPos0].ypos
			temp2 = object[arrayPos0].ypos
		end if
		if temp1 < temp2
			object[arrayPos0].value2 = temp1
			object[arrayPos0].value3 = temp2
		else
			object[arrayPos0].value2 = temp2
			object[arrayPos0].value3 = temp1
		end if
		object[arrayPos0].value2 -= 0x600000
		object[arrayPos0].value3 += 0x600000
		
		object[arrayPos0].value7 = object[arrayPos0].value3
		object[arrayPos0].value7 -= object[arrayPos0].value2
		object[arrayPos0].value7 >>= 1
		object[arrayPos0].value7 += object[arrayPos0].value2
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
